****************************
**   IV regressions ********
****************************

capt drop _est_*


local Outcomelist collapsed IntoCollapse
foreach outcome of local Outcomelist {

** First stage - reg 1
reghdfe_old `outcome' ///
($Treatment = $Instrument_5  )  ///
, absorb( i.id_eez_tax i.iyear)  cluster($ClusterLevel)  ffirst stages(reduced first) 
eststo IV_`outcome'_h1
gen Sample_IV_`outcome'_h1 =e(sample)
estimates restore reghdfe_first1
distinct eez if Sample_IV_`outcome'_h1==1 
estadd scalar EEZ = r(ndistinct)  , replace
eststo FS_IV_`outcome'_h1


** First stage - reg 2
reghdfe_old `outcome' ///
($Treatment = $Instrument_5 $Instrument_N_5  )  ///
, absorb( i.id_eez_tax i.iyear)  cluster($ClusterLevel)  ffirst stages(reduced first) 
eststo IV_`outcome'_h2
gen Sample_IV_`outcome'_h2 =e(sample)
estimates restore reghdfe_first1
distinct eez if Sample_IV_`outcome'_h2==1
estadd scalar EEZ = r(ndistinct)  , replace
eststo FS_IV_`outcome'_h2


** regression 1
xtset id_eez_tax iyear
reghdfe_old `outcome' ///
($Treatment = $Instrument_N_5  )  ///
, absorb( i.id_eez_tax i.iyear)  cluster($ClusterLevel)  ffirst stages(reduced first) 
eststo IV_`outcome'_1
gen Sample_IV_`outcome'_1 =e(sample)
estimates restore reghdfe_first1
distinct eez if Sample_IV_`outcome'_1==1
estadd scalar EEZ = r(ndistinct)  , replace
eststo FS_IV_`outcome'_1


** regression 2
xtset id_eez_tax iyear
reghdfe_old `outcome' ///
$ControlsShort ///
($Treatment = $Instrument_N_5    )  ///
, absorb( i.id_eez_tax  i.iyear)  cluster($ClusterLevel)  ffirst stages(reduced first) 
eststo IV_`outcome'_2
gen Sample_IV_`outcome'_2 =e(sample)
estimates restore reghdfe_first1
distinct eez if Sample_IV_`outcome'_2==1
estadd scalar EEZ = r(ndistinct)  , replace
eststo FS_IV_`outcome'_2

** regression 3
xtset id_eez_tax iyear
reghdfe_old `outcome' ///
$ControlsMedium ///
($Treatment = $Instrument_N_5  )  ///
, absorb( i.id_eez_tax  i.iyear)  cluster($ClusterLevel)  ffirst stages(reduced first) 
eststo IV_`outcome'_3
gen Sample_IV_`outcome'_3 =e(sample)
estimates restore reghdfe_first1
distinct eez if Sample_IV_`outcome'_3==1
estadd scalar EEZ = r(ndistinct)  , replace
eststo FS_IV_`outcome'_3

** regression 4
xtset id_eez_tax iyear
reghdfe_old `outcome' ///
$ControlsLong ///
($Treatment = $Instrument_N_5  )  ///
, absorb( i.id_eez_tax  i.iyear)  cluster($ClusterLevel)  ffirst stages(reduced first) 
eststo IV_`outcome'_4
gen Sample_IV_`outcome'_4 =e(sample)
estimates restore reghdfe_first1
distinct eez if Sample_IV_`outcome'_4==1
estadd scalar EEZ = r(ndistinct)  , replace
eststo FS_IV_`outcome'_4

}


drop Sample_IV_*



****************************
**   Table: 2nd stage ******
****************************


local Outcomelist collapsed IntoCollapse
foreach outcome of local Outcomelist {

** regression 1
estimates restore  IV_`outcome'_1
estadd scalar F_statistic = e(widstat) , replace
distinct id_eez_tax  if e(sample)==1
estadd scalar K1 = r(ndistinct) , replace
sum iyear if e(sample)==1
estadd  local Period "`r(min)'-`r(max)'"   , replace
sum F1.`outcome' if e(sample)==1 
estadd scalar Mean = r(mean) , replace
estadd  local Controls "No"   , replace
estadd  local FE "EEZ-species"   , replace
distinct eez if e(sample)==1
estadd scalar EEZ = r(ndistinct)  , replace
estadd scalar Stock = e(sstatp) , replace
estadd scalar AR = e(archi2p) , replace

eststo IV_`outcome'_1


** regression 2
estimates restore  IV_`outcome'_2
estadd scalar F_statistic = e(widstat) , replace
distinct id_eez_tax  if e(sample)==1
estadd scalar K1 = r(ndistinct) , replace
sum iyear if e(sample)==1
estadd  local Period "`r(min)'-`r(max)'"   , replace
sum F1.`outcome' if e(sample)==1 
estadd scalar Mean = r(mean) , replace
estadd  local Controls "Short"   , replace
estadd  local FE "EEZ-species"   , replace
distinct eez if e(sample)==1
estadd scalar EEZ = r(ndistinct)  , replace
estadd scalar Stock = e(sstatp) , replace
estadd scalar AR = e(archi2p) , replace
eststo IV_`outcome'_2


** regression 3
estimates restore  IV_`outcome'_3
estadd scalar F_statistic = e(widstat) , replace
distinct id_eez_tax  if e(sample)==1
estadd scalar K1 = r(ndistinct) , replace
sum iyear if e(sample)==1
estadd  local Period "`r(min)'-`r(max)'"   , replace
sum F1.`outcome' if e(sample)==1 
estadd scalar Mean = r(mean) , replace
estadd  local Controls "Medium"   , replace
estadd  local FE "EEZ-species"   , replace
distinct eez if e(sample)==1
estadd scalar EEZ = r(ndistinct)  , replace
estadd scalar Stock = e(sstatp) , replace
estadd scalar AR = e(archi2p) , replace
eststo IV_`outcome'_3


** regression 4
estimates restore  IV_`outcome'_4
estadd scalar F_statistic = e(widstat) , replace
distinct id_eez_tax  if e(sample)==1
estadd scalar K1 = r(ndistinct) , replace
sum iyear if e(sample)==1
estadd  local Period "`r(min)'-`r(max)'"   , replace
sum F1.`outcome' if e(sample)==1 
estadd scalar Mean = r(mean) , replace
estadd  local Controls "Long"   , replace
estadd  local FE "EEZ-species"   , replace
distinct eez if e(sample)==1
estadd scalar EEZ = r(ndistinct)  , replace
estadd scalar Stock = e(sstatp) , replace
estadd scalar AR = e(archi2p) , replace
eststo IV_`outcome'_4

}


** Make a 2nd stage table with both outcome variables

esttab  ///
IV_collapsed_1 ///
IV_collapsed_2 ///
IV_collapsed_3 ///
IV_collapsed_4 ///
using "${tables}IV_collapsed_plain_20170921.tex", ///
keep($Treatment) ///
nomtitles nogap  fragment noobs nonotes nonumbers collabels(none)  noline ///
star(* 0.10 ** 0.05 *** 0.01) ///  
se /*standard errors in parentheses*/   ///
coeflabels( $Treatment "PPR"  ) ///
replace
** MAIN

esttab  ///
IV_IntoCollapse_1 ///
IV_IntoCollapse_2 ///
IV_IntoCollapse_3 ///
IV_IntoCollapse_4 ///
using "${tables}IV_IntoCollapse_plain_20170921.tex", ///
keep($Treatment) ///
nomtitles nogap  fragment noobs nonotes nonumbers collabels(none)  noline ///
star(* 0.10 ** 0.05 *** 0.01) ///  
se /*standard errors in parentheses*/   ///
coeflabels( $Treatment "PPR"  ) ///
replace
** MAIN


estimates restore IV_IntoCollapse_1
sum collapsed if e(sample)==1
estadd scalar MeanCol = r(mean) , replace
sum IntoCollapse if e(sample)==1
estadd scalar MeanIntoCol = r(mean) , replace
eststo IV_IntoCollapse_1_v2
*
estimates restore IV_IntoCollapse_2
sum collapsed if e(sample)==1
estadd scalar MeanCol = r(mean) , replace
sum IntoCollapse if e(sample)==1
estadd scalar MeanIntoCol = r(mean) , replace
eststo IV_IntoCollapse_2_v2
*
estimates restore IV_IntoCollapse_3
sum collapsed if e(sample)==1
estadd scalar MeanCol = r(mean) , replace
sum IntoCollapse if e(sample)==1
estadd scalar MeanIntoCol = r(mean) , replace
eststo IV_IntoCollapse_3_v2
*
estimates restore IV_IntoCollapse_4
sum collapsed if e(sample)==1
estadd scalar MeanCol = r(mean) , replace
sum IntoCollapse if e(sample)==1
estadd scalar MeanIntoCol = r(mean) , replace
eststo IV_IntoCollapse_4_v2


esttab  ///
IV_IntoCollapse_1_v2 ///
IV_IntoCollapse_2_v2   ///
IV_IntoCollapse_3_v2 ///
IV_IntoCollapse_4_v2 ///
using "${tables}IV_info_20170921.tex", ///
drop(*) ///
nomtitles nogap  fragment  noline nonumbers ///
star(* 0.10 ** 0.05 *** 0.01) ///  
stats(Controls   N Period N_clust EEZ K1 MeanCol MeanIntoCol F_statistic ///
,label( "Controls (time-varying)"   "Obs" "Period" ///
 "Countries" "EEZs"  "EEZ-species" "Mean collapse (persistent)" "Mean collapse (transition)" ///
"Weak-ID test"  )) ///
se /*standard errors in parentheses*/   ///
coeflabels( $Treatment "PPR"  ) ///
replace
** MAIN


*********  Anderson-Rubin p-value  *********

esttab  ///
IV_collapsed_1 ///
IV_collapsed_2   ///
IV_collapsed_3 ///
IV_collapsed_4 ///
using "${tables}IV_info_AR_collapsed.tex", ///
drop(*)  ///
nomtitles nogap  fragment  noline nonumbers ///
stats(AR ,label("Anderson-Rubin p-value (persistent)")) ///
se /*standard errors in parentheses*/   ///
replace
** MAIN

esttab  ///
IV_IntoCollapse_1 ///
IV_IntoCollapse_2   ///
IV_IntoCollapse_3 ///
IV_IntoCollapse_4 ///
using "${tables}IV_info_AR_IntoCollapsed.tex", ///
drop(*)  ///
nomtitles nogap  fragment  noline nonumbers ///
stats(AR ,label("Anderson-Rubin p-value (transition)")) ///
se /*standard errors in parentheses*/   ///
replace
** MAIN



*********  Anderson-Rubin CI  *********
preserve
tab iyear, generate(DummyYear)

local Outcomelist collapsed IntoCollapse
foreach outcome of local Outcomelist {

xtset id_eez_tax iyear
weakiv xtivreg2 `outcome' DummyYear*  ///
($Treatment = $Instrument_N_5  ) ///
, fe  cluster($ClusterLevel) first ffirst rf savefirst ///
partial(DummyYear*)  md
estadd  local AR_CSET "`e(ar_cset)'"   , replace
eststo WeakIV_`outcome'_1

xtset id_eez_tax iyear
weakiv xtivreg2 `outcome' DummyYear*  ///
$ControlsShort ///
($Treatment = $Instrument_N_5  ) ///
, fe  cluster($ClusterLevel) first ffirst rf savefirst ///
partial(DummyYear*)  md
estadd  local AR_CSET "`e(ar_cset)'"   , replace
eststo WeakIV_`outcome'_2

xtset id_eez_tax iyear
weakiv xtivreg2 `outcome' DummyYear*  ///
$ControlsMedium ///
($Treatment = $Instrument_N_5  ) ///
, fe  cluster($ClusterLevel) first ffirst rf savefirst ///
partial(DummyYear*)  md
estadd  local AR_CSET "`e(ar_cset)'"   , replace
eststo WeakIV_`outcome'_3

xtset id_eez_tax iyear
weakiv xtivreg2 `outcome' DummyYear*  ///
$ControlsLong ///
($Treatment = $Instrument_N_5  ) ///
, fe  cluster($ClusterLevel) first ffirst rf savefirst ///
partial(DummyYear*)  md
estadd  local AR_CSET "`e(ar_cset)'"   , replace
eststo WeakIV_`outcome'_4
}
restore



* Example: e(AR_CSET) : "[-.262771,-.014972]"
**** Manually remove 3 decimals from the CI!
esttab  ///
WeakIV_collapsed_1 ///
WeakIV_collapsed_2   ///
WeakIV_collapsed_3 ///
WeakIV_collapsed_4 ///
using "${tables}IV_info_ARCSET_collapsed.tex", ///
nomtitles nogap  fragment  noline nonumbers ///
stats(AR_CSET ,label( "Anderson-Rubin CI (persistent)")) ///
se   ///
replace


**** Manually remove 3 decimals from the CI!
esttab  ///
WeakIV_IntoCollapse_1 ///
WeakIV_IntoCollapse_2   ///
WeakIV_IntoCollapse_3 ///
WeakIV_IntoCollapse_4 ///
using "${tables}IV_info_ARCSET_IntoCollapsed.tex", ///
nomtitles nogap  fragment  noline nonumbers ///
stats(AR_CSET ,label( "Anderson-Rubin CI (transition)")) ///
se   ///
replace


************************************
************************************


****************************
**   Table: 1st stage ******
****************************


local Outcomelist collapsed 
foreach outcome of local Outcomelist {


** regression h1
estimates restore  FS_IV_`outcome'_h1
estadd scalar F_statistic = e(first_F) , replace
estadd scalar K1 = e(M_due_to_nested) , replace
estadd  local Controls "No"   , replace
estadd  local FE "EEZ-species"   , replace
eststo FS_IV_`outcome'_h1


** regression h2
estimates restore  FS_IV_`outcome'_h2
estadd scalar F_statistic = e(first_F) , replace
estadd scalar K1 = e(M_due_to_nested) , replace
estadd  local Controls "No"   , replace
estadd  local FE "EEZ-species"   , replace
eststo FS_IV_`outcome'_h2


** regression 1
estimates restore  FS_IV_`outcome'_1
estadd scalar F_statistic = e(first_F) , replace
estadd scalar K1 = e(M_due_to_nested) , replace
estadd  local Controls "No"   , replace
estadd  local FE "EEZ-species"   , replace
eststo FS_IV_`outcome'_1


** regression 2
estimates restore  FS_IV_`outcome'_2
estadd scalar F_statistic = e(first_F) , replace
estadd scalar K1 = e(M_due_to_nested) , replace
sum iyear if e(sample)==1
estadd  local Period "`r(min)'-`r(max)'"   , replace
sum $Instrument_N_5 if e(sample)==1 
estadd scalar Mean = r(mean) , replace
estadd  local Controls "Short"   , replace
estadd  local FE "EEZ-species"   , replace
eststo FS_IV_`outcome'_2


** regression 3
estimates restore  FS_IV_`outcome'_3
estadd scalar F_statistic = e(first_F) , replace
estadd scalar K1 = e(M_due_to_nested) , replace
sum iyear if e(sample)==1
estadd  local Period "`r(min)'-`r(max)'"   , replace
sum $Instrument_N_5 if e(sample)==1 
estadd scalar Mean = r(mean) , replace
estadd  local Controls "Medium"   , replace
estadd  local FE "EEZ-species"   , replace
eststo FS_IV_`outcome'_3


** regression 4
estimates restore  FS_IV_`outcome'_4
estadd scalar F_statistic = e(first_F) , replace
estadd scalar K1 = e(M_due_to_nested) , replace
sum iyear if e(sample)==1
estadd  local Period "`r(min)'-`r(max)'"   , replace
sum $Instrument_N_5 if e(sample)==1 
estadd scalar Mean = r(mean) , replace
estadd  local Controls "Long"   , replace
estadd  local FE "EEZ-species"   , replace
eststo FS_IV_`outcome'_4

}


** Make a 1st stage table 

esttab  ///
FS_IV_collapsed_h1 ///
FS_IV_collapsed_h2 ///
FS_IV_collapsed_1 ///
FS_IV_collapsed_2 ///
FS_IV_collapsed_3 ///
FS_IV_collapsed_4 ///
using "${tables}FS_IV_plain_20170921.tex", ///
keep(*ppr*) ///
nomtitles nogap  fragment noobs nonotes nonumbers collabels(none)  noline ///
star(* 0.10 ** 0.05 *** 0.01) ///  
se /*standard errors in parentheses*/   ///
coeflabels( L5.ppr_other_num_time "PPR other areas" ///
L5.ppr_other_num_time_neighbour  "PPR other areas, spatial lag" ) ///
replace
** MAIN


esttab  ///
FS_IV_collapsed_h1 ///
FS_IV_collapsed_h2 ///
FS_IV_collapsed_1 ///
FS_IV_collapsed_2 ///
FS_IV_collapsed_3 ///
FS_IV_collapsed_4 ///
using "${tables}FS_IV_info_20170921.tex", ///
drop(*) ///
nomtitles nogap  fragment  noline nonumbers ///
star(* 0.10 ** 0.05 *** 0.01) ///  
stats(Controls   N  N_clust  EEZ K1  F_statistic ///
,label( "Controls (time-varying)"   "Obs"  ///
 "Countries" "EEZs"  "EEZ-species"  "F-stat excl. instr." )) ///
se /*standard errors in parentheses*/   ///
coeflabels( $Treatment "PPR"  ) ///
replace
** MAIN


************************************
************************************

capt drop _est_*

